#!/bin/bash

# GuDB Complete Build Script
# 一键完成所有构建和准备工作

set -e

echo "🚀 GuDB Complete Build & Setup Script"
echo "======================================"

# Check if we're in the correct directory
if [ ! -f "README.md" ] || [ ! -d "build" ]; then
    echo "❌ Error: Please run this script from the GuDB root directory"
    exit 1
fi

# Check if make is available
if ! command -v make &> /dev/null; then
    echo "❌ Error: make command not found. Please install build tools."
    echo "   Ubuntu/Debian: sudo apt-get install build-essential"
    echo "   CentOS/RHEL: sudo yum groupinstall 'Development Tools'"
    echo "   macOS: xcode-select --install"
    exit 1
fi

# Check if gcc is available
if ! command -v gcc &> /dev/null; then
    echo "❌ Error: gcc compiler not found. Please install GCC."
    exit 1
fi

# Check for zlib
if ! pkg-config --exists zlib 2>/dev/null; then
    echo "⚠️  Warning: zlib not found via pkg-config"
    echo "   If build fails, install zlib-dev:"
    echo "   Ubuntu/Debian: sudo apt-get install zlib1g-dev"
    echo "   CentOS/RHEL: sudo yum install zlib-devel"
    echo "   macOS: brew install zlib"
fi

echo ""
echo "🧹 Cleaning previous build..."
(cd build && make clean 2>/dev/null) || true

echo ""
echo "🔨 Building GuDB main library..."
if (cd build && make 2>&1); then
    if [ -f "build/libgudb.a" ]; then
        echo "✅ Main library build successful"
        if [ -f "build/libgudb.so" ]; then
            echo "✅ Shared library also created"
        fi
    else
        echo "❌ Main library build failed - libgudb.a not found"
        exit 1
    fi
else
    # Check if libraries were created despite warnings
    if [ -f "build/libgudb.a" ]; then
        echo "⚠️  Build completed with warnings but libraries created"
        echo "✅ Main library build successful"
        if [ -f "build/libgudb.so" ]; then
            echo "✅ Shared library also created"
        fi
    else
        echo "❌ Main library build failed"
        exit 1
    fi
fi

echo ""
echo "🔧 Fixing integration test include paths..."
# Fix include paths in integration tests
for file in tests/integration/*.c; do
    if [ -f "$file" ]; then
        sed -i.bak 's/#include "include\/gudb.h"/#include "gudb.h"/g' "$file" 2>/dev/null || true
        sed -i.bak 's/#include "src\/.*\/\([^"]*\)"/#include "\1"/g' "$file" 2>/dev/null || true
        rm -f "$file.bak" 2>/dev/null || true
    fi
done

echo ""
echo "🧪 Building integration tests..."
if make -C tests/integration LIBPATH=../../build LIBNAME=gudb 2>/dev/null; then
    echo "✅ Integration tests build successful"
else
    echo "⚠️  Some integration tests may have build issues, but continuing..."
fi

echo ""
echo "📚 Building examples..."
if make -C examples LIBPATH=../build LIBNAME=gudb 2>/dev/null; then
    echo "✅ Examples build successful"  
else
    echo "⚠️  Some examples may have build issues, but continuing..."
fi

echo ""
echo "🎉 Build completed successfully!"
echo ""
echo "📁 Generated files:"
echo "   - build/libgudb.a (main static library)"
if [ -f "build/libgudb.so" ]; then
    echo "   - build/libgudb.so (shared library)"
fi
echo "   - tests/integration/* (test executables)"
echo "   - examples/* (example programs)"
echo ""

echo "🚀 Quick start commands:"
echo "   ./scripts/quickstart.sh                    # Start server demo"
echo "   ./examples/tutorial_basic                  # Run basic tutorial"
echo "   ./tests/integration/test_simple            # Test basic functionality"  
echo "   mysql -h localhost -P 3316 -u admin -padmin  # Connect with MySQL client"
echo ""

echo "📖 For detailed documentation, see: docs/complete-guide.md"
echo ""
echo "✨ GuDB is ready to use!"